package cn.itcast.crm.dao;



import cn.itcast.crm.domain.Customer;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import javax.ws.rs.PathParam;
import java.util.List;

/**
 * 定区关联客户服务接口
 * 查询未分配定区客户
 * 查询客户属于哪个定区
 * 分派客户到定区
 *
 */
public interface CustomerRepository extends JpaRepository<Customer, Integer> {
    //注意 find语句编写必须遵jpa Repository查询规范 否则不能正确识别
	List<Customer> findByFixedAreaIdIsNull();

	Customer findByTelephone(String telephone);

	List<Customer> findByFixedAreaId(String fixedAreaId);

	//批量关联未绑定客户ID到指定定区
	@Query("update Customer set fixedAreaId = :fid where id in:orphanIds")
	@Modifying
	void associate(@Param("orphanIds") List<Integer> orphanIds,@Param("fid") String fixedAreaId);

	@Query("update Customer set fixedAreaId = null where id in:customerIds")
	@Modifying
	void unassociate(@Param("customerIds")List<Integer> customerIds);

	@Query("update Customer set type = 1 where telephone =:telephone")
	@Modifying
	void updateType(@Param("telephone") String telephone);

}
